package com.wanmait.shoppingsys.controller.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wanmait.shoppingsys.dao.SpecialtyDAO;
import com.wanmait.shoppingsys.util.Pager;
import com.wanmait.shoppingsys.vo.Province;
import com.wanmait.shoppingsys.vo.Specialty;

@WebServlet("/SpecialtyController")
public class SpecialtyController extends HttpServlet {
	private SpecialtyDAO specialtyDAO = SpecialtyDAO.getDAO();
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String action = request.getParameter("action");
		if(action==null) return;
		switch(action)
		{
//		case "findbycity":      // 没写!!!!!!!!!
//			this.findbycity(request, response);
//			break;
		case "serach":          //关键字搜索
			this.serach(request,response);
			break;
		case "detailed":          //商品详细信息展示
			this.detailed(request,response);//(detailed:详细的意思)
			break;
		case "produitbyprice":         //关键字搜索按价格排序
			this.produitbyprice(request, response);
			break;
		case "produitbylike":        //按照人气排序
			this.produitbylike(request,response);
			break;	
		case "findAll":        //此种类 全部商品
			this.findAll(request,response);
			break;
		case "typebyprice":        //   类型搜索价格排序
			this.typebyprice(request,response);
			break;
		case "typebylike":        //   类型搜索人气排序
			this.typebylike(request,response);
			break;
		}

	}
	protected void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{   
		       Pager pager = Pager.inita(request); 
		       specialtyDAO.findAll(pager);
		       request.getRequestDispatcher("/specialty/serachlist.jsp").forward(request, response);
		       
	}
	
//	protected void findbycity(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
//	{
//		String name = request.getParameter("name");
//		String cityIdStr = request.getParameter("cityId");
//		Integer cityId = Integer.parseInt(cityIdStr);
//		
//		List<Specialty> specialtys = specialtyDAO.findbycity(name, cityId);
//		
//		request.setAttribute("specialtys", specialtys);
//		request.getRequestDispatcher("/specialty/findbycity.jsp").forward(request, response);
//		
//		
//	}
	
	protected void serach(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		Pager pager = Pager.init(request);              //关键字搜索
		specialtyDAO.produitSelect(pager);
		request.getRequestDispatcher("/specialty/serachlist.jsp").forward(request, response);
	}

	
	
	protected void produitbyprice(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{   
		       Pager pager = Pager.init(request); 
		       specialtyDAO.produitByPrice(pager);
		       request.getRequestDispatcher("/specialty/serachlist.jsp").forward(request, response);
		       
	}
	protected void produitbylike(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{   
		       Pager pager = Pager.init(request); 
		       specialtyDAO.produitByLike(pager);
		       request.getRequestDispatcher("/specialty/serachlist.jsp").forward(request, response);
		       
	}

	protected void detailed(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		String idStr = request.getParameter("id");      //接收搜索传过来的 specialty数据库表的Id
		Integer id = Integer.parseInt(idStr);
		Specialty specialty = specialtyDAO.findById(id);
		request.setAttribute("specialty", specialty);
		request.getRequestDispatcher("/specialty/show.jsp").forward(request, response);
		
	}
	protected void typebyprice(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{   
		       Pager pager = Pager.inita(request); 
		       specialtyDAO.typeByPrice(pager);
		       request.getRequestDispatcher("/specialty/serachlist.jsp").forward(request, response);
		       
	}
	
	protected void typebylike(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{   
		       Pager pager = Pager.inita(request); 
		       specialtyDAO.typeByLike(pager);
		       request.getRequestDispatcher("/specialty/serachlist.jsp").forward(request, response);
		       
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}
